home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / info-1.000 / info-1 / info / info.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-27  |  4.4 KB  |  187 lines

  1. /* Linux 1.2.13 (Slackware 3.0) Includes */
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/file.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/sysmacros.h>
  9. #include <pwd.h>
  10. #include <grp.h>
  11. #include <time.h>
  12. #include <string.h>
  13.  
  14. /* my includes */
  15. #include "info.h"
  16.  
  17. /* functions */
  18. void main(int argc, char *argv[])
  19. {
  20.    int fd=0;                  
  21.    struct stat buf={0};      
  22.  
  23.    if(argc!=2) help(2,NULL); 
  24.    if(argv[1][0]=='-') help(1,NULL);
  25.    if(access(argv[1],R_OK)==0)
  26.       fd=open(argv[1],O_RDONLY,0);
  27.    else
  28.       help(3,argv[1]);
  29.    fstat(fd,&buf); 
  30.    print(buf);    
  31.    exit(0);
  32. }
  33.  
  34. void print(struct stat buf)
  35. {
  36.    dev_t maj_dev=0,min_dev=0;
  37.    mode_t operm=0;
  38.    char perms[15]={0};
  39.    char uname[9]={0};
  40.    char gname[9]={0};
  41.    struct tm *time=NULL;
  42.    
  43.  
  44.    if (buf.st_rdev)
  45.    {
  46.       maj_dev=major(buf.st_rdev);
  47.       min_dev=minor(buf.st_rdev);
  48.    }
  49.  
  50.    fprintf(stdout,"info, version "VERSION"\n"
  51.                   "    Copyright (c) 1996 Peter M. Jones aka ToneDef, <1-9951@tnet.bluethun.com>\n"
  52.                   "\n");
  53.    if (buf.st_rdev)
  54.       fprintf(stdout,
  55.                   "Device:            Yes.\n"
  56.                   "   Major:          %d\n"
  57.                   "   Minor:          %d\n",maj_dev,min_dev);
  58.    else
  59.       fprintf(stdout,
  60.                   "Device:            No.\n");
  61.    fprintf(stdout,"Inode:             %ld\n",buf.st_ino);
  62.    set_perms(perms,buf);
  63.    operm=set_octal(buf.st_mode);
  64.    fprintf(stdout,"Mode:              (%s) (0%o)\n",perms, operm);
  65.    if (perms[0]=='d')
  66.       fprintf(stdout,"Subdirectories:    %d\n",buf.st_nlink);
  67.    else
  68.       fprintf(stdout,"Links to file:     %d\n",buf.st_nlink);
  69.    getnames(buf.st_uid,buf.st_gid, uname,gname);
  70.    fprintf(stdout,"Owner Name:        %s\n"
  71.                   "Group Name:        %s\n",uname,gname);
  72.    fprintf(stdout,"Size:              %ld\n",(long int)buf.st_size);
  73.    time=localtime(&buf.st_atime);
  74.    fprintf(stdout,"Last accessed:     %s",asctime(time));
  75.    time=localtime(&buf.st_mtime);
  76.    fprintf(stdout,"Last modified:     %s",asctime(time));
  77.    time=localtime(&buf.st_ctime);
  78.    fprintf(stdout,"Last inode change: %s",asctime(time));
  79. }
  80.  
  81. void getnames(long int uid, long int gid, char *uname, char *gname)
  82. {
  83.    struct passwd *pw=NULL;
  84.    struct group *gpw=NULL;
  85.  
  86.    pw=getpwuid(uid);
  87.    strcpy(uname,pw->pw_name);
  88.    gpw=getgrgid(gid);
  89.    strcpy(gname,gpw->gr_name);
  90. }
  91.  
  92. void set_perms(char *perms, struct stat buf)
  93. {
  94.    char *modes[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
  95.    int i=0, j=0;
  96.   
  97.    *perms = '\0';
  98.    strcat(perms,"\0\0\0\0\0\0\0\0\0\0\0\0\0");
  99.    switch (buf.st_mode & S_IFMT)
  100.    {
  101.       case S_IFREG:
  102.          perms[0]='-'; 
  103.      break;
  104.       case S_IFDIR:
  105.          perms[0]='d';
  106.          break;
  107.       case S_IFCHR:
  108.      perms[0]='c';
  109.          break;
  110.       case S_IFBLK:
  111.          perms[0]='b';
  112.          break;
  113.       case S_IFLNK:
  114.          perms[0]='l';
  115.          break;
  116.       case S_IFSOCK:
  117.          perms[0]='s';
  118.          break;
  119.       case S_IFIFO:
  120.          perms[0]='f';
  121.          break;
  122.       default:
  123.          perms[0]='?';
  124.          break;
  125.    }
  126.    for(i=2; i>=0; i--)
  127.    {
  128.       j = (buf.st_mode >> (i*3)) & 07;
  129.       strcat(perms, modes[j]);
  130.    }
  131.    if (perms[3]=='x')
  132.       perms[11]='x';
  133.  
  134.    if (perms[6]=='x')
  135.       perms[12]='x';
  136.  
  137.    if (perms[9]=='x')
  138.       perms[13]='x';
  139.  
  140.    if ((buf.st_mode & S_ISUID) != 0)
  141.    {
  142.      if(buf.st_uid==0)
  143.         perms[3] = 'S';
  144.      else
  145.         perms[3] = 's';
  146.    }
  147.    if ((buf.st_mode & S_ISGID) != 0)
  148.    {
  149.      if(buf.st_uid==0)
  150.         perms[6] = 'S';
  151.      else
  152.         perms[6] = 's';
  153.    }
  154.    if ((buf.st_mode & S_ISVTX) != 0)
  155.       perms[9] = 't';
  156. }
  157.  
  158. mode_t set_octal(mode_t oct)
  159. {
  160.    oct&=(~S_IFMT);
  161.    return(oct);
  162. }
  163.  
  164. void help(int exitval, char *fname)
  165. {
  166.    FILE *fp=NULL;
  167.    
  168.    if(exitval<3)
  169.    {
  170.       fp=fdopen(exitval,"w");
  171.       fprintf(fp,"info, version "VERSION"\n"
  172.                  "    Copyright (c) 1996 Peter M. Jones aka ToneDef, <1-9951@tnet.bluethun.com>\n"
  173.                  "\n"
  174.                  "usage: info filename\n");
  175.       exit(exitval-1);
  176.    } else {
  177.       fprintf(stderr,"info, version "VERSION"\n"
  178.                      "    Copyright (c) 1996 Peter M. Jones aka ToneDef, <1-9951@tnet.bluethun.com>\n"
  179.                      "\n"
  180.                      "ERROR: file \"%s\" is not readable.\n"
  181.                      "  Check to be sure that the +r permission is set, "
  182.                      "and that the file exists.\n", 
  183.                      fname);
  184.       exit(1);
  185.    }
  186. }
  187.